package com.yz.dao;

import com.yz.pojo.Department;
import com.yz.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuanzheng
 * @date 2020/7/22-14:20
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<>();
        employees.put(1000, new Employee(1000, "a", "A123654@163.com", 1, new Department(100, "ceshi1")));
        employees.put(1001, new Employee(1001, "b", "B123654@163.com", 0, new Department(101, "ceshi2")));
        employees.put(1002, new Employee(1002, "c", "C123654@163.com", 1, new Department(102, "ceshi3")));
        employees.put(1003, new Employee(1003, "d", "D123654@163.com", 0, new Department(103, "ceshi4")));
    }

    private Integer id = 1004;

    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(id++);
        }
        //employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    public Collection<Employee> getEmployees() {
        return employees.values();
    }

    public Employee getEmployeeById(Integer id) {
        return employees.get(id);
    }
}
